#-*-coding:utf-8-*-
import os
import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *

class SetProjectPath(QWidget):
    project_db_path_Signal=Signal(str)
    def __init__(self):
        super(SetProjectPath,self).__init__()

        self.setWindowTitle('设置项目引用路径')
        self.mainLayout=QHBoxLayout()

        # self.path_layout=QHBoxLayout()
        self.path_label=QLabel('选择路径:')
        self.path_edit=QLineEdit()
        self.path_btn=QPushButton('...')
        self.path_btn.setMaximumWidth(25)
        self.path_btn.clicked.connect(self.__chooseDir)
        self.OK_btn=QPushButton('设置')
        self.OK_btn.clicked.connect(self.__setProjectPath)
        self.OK_btn.setMaximumWidth(40)
        self.NO_btn=QPushButton('取消')
        self.NO_btn.clicked.connect(self.__close)
        self.NO_btn.setMaximumWidth(40)
        self.mainLayout.addWidget(self.path_label)
        self.mainLayout.addWidget(self.path_edit)
        self.mainLayout.addWidget(self.path_btn)
        self.mainLayout.addWidget(self.OK_btn)
        self.mainLayout.addWidget(self.NO_btn)
        self.setLayout(self.mainLayout)

    def __chooseDir(self):
        dir=QFileDialog.getExistingDirectory(self,'选择目录')
        self.path_edit.setText(dir)

    def __setProjectPath(self):
        dir=self.path_edit.text()
        self.project_db_path_Signal.emit(dir)
        self.__writeProjectPath(dir)
        self.close()

    def __close(self):
        self.close()

    def __writeProjectPath(self,path):
        config_path= os.path.join(os.path.dirname(os.path.dirname(__file__)),'config')
        projectPath=os.path.join(config_path,'project.info')

        with open(projectPath,'w',encoding='utf-8') as w:
            w.write(path)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    s=SetProjectPath()
    s.show()
    sys.exit(app.exec_())